package cliente;

import java.rmi.RemoteException;
import java.util.HashSet;

public class IU_Espera extends javax.swing.JFrame {

    // Inicialização de instância única de IU_Espera
    private static final IU_Espera theIU_Espera = new IU_Espera();
    private HashSet<String> jogadoresConectados;

    public void iniciarPartida() {        
        IU_Jogo.getIU_Jogo().atualizarTelaDeJogo(); 
        fecharTela();
    }

    // Construtor privado
    private IU_Espera(){
        initComponents();
    }

    // Ponto de acesso à instância única
    public static IU_Espera getIU_Espera(){
        return theIU_Espera;
    }
   
    public void exibirTela(){
        getIU_Espera().setVisible(true);
    }
    
    public void fecharTela(){
        getIU_Espera().dispose();
    }

    public HashSet<String> getJogadoresConectados() {
        return jogadoresConectados;
    }

    public void setJogadoresConectados(HashSet<String> jogadoresConectados) {
        this.jogadoresConectados = jogadoresConectados;
    }    
    
    public void exibirJogadoresConectados(HashSet<String> jogadoresConectados){
         /* Chamado por: SO_PresidenteCliente, VF1
         * Caso de uso: Notificar Espera */
        setJogadoresConectados(jogadoresConectados);
        listaJogadoresConectados_Espera.setModel(new javax.swing.AbstractListModel() {
            public int getSize() { return getJogadoresConectados().size(); }
            public Object getElementAt(int i) { return getJogadoresConectados().toArray()[i]; }
        });
    }
    public void sairDaEspera() throws RemoteException{
         /* Chamado por: Jogador
          * Caso de uso: Sair da espera, VF1 */
         SO_PresidenteCliente.getSO_PresidenteCliente().desconectarJogadores();
    }
    
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        jScrollPane1 = new javax.swing.JScrollPane();
        listaJogadoresConectados_Espera = new javax.swing.JList();
        jMenuBar1 = new javax.swing.JMenuBar();
        menuJogo_Espera = new javax.swing.JMenu();
        subMenuSair_Espera = new javax.swing.JMenuItem();

        setDefaultCloseOperation(javax.swing.WindowConstants.DO_NOTHING_ON_CLOSE);
        setTitle("Espera");
        setName("frame_Espera"); // NOI18N
        setResizable(false);

        jPanel1.setBackground(new java.awt.Color(51, 153, 0));

        jLabel1.setForeground(new java.awt.Color(255, 255, 255));
        jLabel1.setText("Lista de jogadores conectados");

        jScrollPane1.setViewportView(listaJogadoresConectados_Espera);

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 307, Short.MAX_VALUE)
                    .addComponent(jLabel1))
                .addContainerGap())
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel1)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 80, Short.MAX_VALUE)
                .addContainerGap())
        );

        menuJogo_Espera.setText("Jogo");

        subMenuSair_Espera.setText("Sair");
        subMenuSair_Espera.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                subMenuSair_EsperaActionPerformed(evt);
            }
        });
        menuJogo_Espera.add(subMenuSair_Espera);

        jMenuBar1.add(menuJogo_Espera);

        setJMenuBar(jMenuBar1);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

private void subMenuSair_EsperaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_subMenuSair_EsperaActionPerformed
    fecharTela();
    IU_Inicial.getIU_Inicial().habilitarTela();
}//GEN-LAST:event_subMenuSair_EsperaActionPerformed

    /**
    * @param args the command line arguments
    */
//    public static void main(String args[]) {
//        IU_Espera.getIU_Espera().exibirTela();
//    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JLabel jLabel1;
    private javax.swing.JMenuBar jMenuBar1;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JList listaJogadoresConectados_Espera;
    private javax.swing.JMenu menuJogo_Espera;
    private javax.swing.JMenuItem subMenuSair_Espera;
    // End of variables declaration//GEN-END:variables

}
